********************************************************************************
** 	TITLE:		us2008_anes_contact_networks                                  ** 	
**  AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       anes_timeseries_2008                                          **
**  DATE:	    October 2022 						                          **	
**	VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "us2008_anes_contact_networks", replace

* Import knowledge question answers from Excel sheets

copy https://electionstudies.org/wp-content/uploads/2008/03/anes_timeseries_2008_openends_redacted_Dec2012Revision.xls anes_timeseries_2008_openends_redacted_Dec2012Revision.xls, replace
type anes_timeseries_2008_openends_redacted_Dec2012Revision.xls
import excel anes_timeseries_2008_openends_redacted_Dec2012Revision.xls, cellrange(A3:D2324) sheet(OfcPelosi) clear

drop C

rename B postelection
rename A V080001
rename D V085120_answer

gen V085120_imported = .

replace V085120_imported = 1 if strpos(V085120_answer, "speaker") > 0
replace V085120_imported = 1 if strpos(V085120_answer, "Speaker") > 0
replace V085120_imported = 1 if strpos(V085120_answer, "SPEAKER") > 0

replace V085120_imported = 0 if V085120_imported!=1
replace V085120_imported = . if postelection == 0

drop V085120_answer postelection

save "V085120_imported.dta", replace

copy https://electionstudies.org/wp-content/uploads/2008/03/anes_timeseries_2008_openends_redacted_Dec2012Revision.xls anes_timeseries_2008_openends_redacted_Dec2012Revision.xls, replace
type anes_timeseries_2008_openends_redacted_Dec2012Revision.xls
import excel anes_timeseries_2008_openends_redacted_Dec2012Revision.xls, cellrange(A3:D2323) sheet(OfcCheney) clear

drop C

rename B postelection
rename A V080001
rename D V085121_answer

gen V085121_imported = .

replace V085121_imported = 1 if strpos(V085121_answer, "vice") > 0
replace V085121_imported = 1 if strpos(V085121_answer, "Vice") > 0
replace V085121_imported = 1 if strpos(V085121_answer, "VICE") > 0

replace V085121_imported = 0 if V085121_imported!=1
replace V085121_imported = . if postelection == 0

drop V085121_answer postelection

save "V085121_imported.dta", replace

copy https://electionstudies.org/wp-content/uploads/2008/03/anes_timeseries_2008_openends_redacted_Dec2012Revision.xls anes_timeseries_2008_openends_redacted_Dec2012Revision.xls, replace
type anes_timeseries_2008_openends_redacted_Dec2012Revision.xls
import excel anes_timeseries_2008_openends_redacted_Dec2012Revision.xls, cellrange(A3:D2324) sheet(OfcBrown) clear

drop C

rename B postelection
rename A V080001
rename D V085122_answer

gen V085122_imported = .

replace V085122_imported = 1 if strpos(V085122_answer, "england") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "England") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "ENGLAND") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "britain") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "Britain") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "BRITAIN") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "kingdom") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "Kingdom") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "KINGDOM") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "english") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "English") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "ENGLISH") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "british") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "British") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "BRITISH") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "uk") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "Uk") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "UK") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "gb") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "Gb") > 0
replace V085122_imported = 1 if strpos(V085122_answer, "GB") > 0

replace V085122_imported = 0 if V085122_imported!=1
replace V085122_imported = . if postelection == 0

drop V085122_answer postelection

save "V085122_imported.dta", replace
 
copy https://electionstudies.org/wp-content/uploads/2008/03/anes_timeseries_2008_openends_redacted_Dec2012Revision.xls anes_timeseries_2008_openends_redacted_Dec2012Revision.xls, replace
type anes_timeseries_2008_openends_redacted_Dec2012Revision.xls
import excel anes_timeseries_2008_openends_redacted_Dec2012Revision.xls, cellrange(A3:D2323) sheet(OfcRoberts) clear

drop C

rename B postelection
rename A V080001
rename D V085123_answer

gen V085123_imported = .

replace V085123_imported = 1 if strpos(V085123_answer, "justice") > 0
replace V085123_imported = 1 if strpos(V085123_answer, "Justice") > 0
replace V085123_imported = 1 if strpos(V085123_answer, "JUSTICE") > 0
replace V085123_imported = 1 if strpos(V085123_answer, "supreme") > 0
replace V085123_imported = 1 if strpos(V085123_answer, "Supreme") > 0
replace V085123_imported = 1 if strpos(V085123_answer, "SUPREME") > 0

replace V085123_imported = 0 if V085123_imported!=1
replace V085123_imported = . if postelection == 0

drop V085123_answer postelection

save "V085123_imported.dta", replace

* Open the dataset

use "anes_timeseries_2008.dta", clear

* Merge knowledge question answers from Excel sheets

merge m:1 V080001 using V085120_imported
rename _merge merge1

merge m:1 V080001 using V085121_imported
rename _merge merge2

merge m:1 V080001 using V085122_imported
rename _merge merge3

merge m:1 V080001 using V085123_imported
rename _merge merge4

drop merge1 merge2 merge3 merge4


**************************
** STATE-LEVEL OUTCOMES **
**************************

* Note: 1 = Democrat, 2 = Republican

gen statewinner = .

replace statewinner = 2 if V081201b == 1
replace statewinner = 2 if V081201b == 2
replace statewinner = 2 if V081201b == 4
replace statewinner = 2 if V081201b == 5
replace statewinner = 1 if V081201b == 6
replace statewinner = 1 if V081201b == 8
replace statewinner = 1 if V081201b == 9
replace statewinner = 1 if V081201b == 10
replace statewinner = 1 if V081201b == 11
replace statewinner = 1 if V081201b == 12
replace statewinner = 2 if V081201b == 13
replace statewinner = 1 if V081201b == 15
replace statewinner = 2 if V081201b == 16
replace statewinner = 1 if V081201b == 17
replace statewinner = 1 if V081201b == 18
replace statewinner = 1 if V081201b == 19
replace statewinner = 2 if V081201b == 20
replace statewinner = 2 if V081201b == 21
replace statewinner = 2 if V081201b == 22
replace statewinner = 1 if V081201b == 23
replace statewinner = 1 if V081201b == 24
replace statewinner = 1 if V081201b == 25
replace statewinner = 1 if V081201b == 26
replace statewinner = 1 if V081201b == 27
replace statewinner = 2 if V081201b == 28
replace statewinner = 2 if V081201b == 29
replace statewinner = 2 if V081201b == 30
replace statewinner = 2 if V081201b == 31
replace statewinner = 1 if V081201b == 32
replace statewinner = 1 if V081201b == 33
replace statewinner = 1 if V081201b == 34
replace statewinner = 1 if V081201b == 35
replace statewinner = 1 if V081201b == 36
replace statewinner = 1 if V081201b == 37
replace statewinner = 2 if V081201b == 38
replace statewinner = 1 if V081201b == 39
replace statewinner = 2 if V081201b == 40
replace statewinner = 1 if V081201b == 41
replace statewinner = 1 if V081201b == 42
replace statewinner = 1 if V081201b == 44
replace statewinner = 2 if V081201b == 45
replace statewinner = 2 if V081201b == 46
replace statewinner = 2 if V081201b == 47
replace statewinner = 2 if V081201b == 48
replace statewinner = 2 if V081201b == 49
replace statewinner = 1 if V081201b == 50
replace statewinner = 1 if V081201b == 51
replace statewinner = 1 if V081201b == 53
replace statewinner = 2 if V081201b == 54
replace statewinner = 1 if V081201b == 55
replace statewinner = 2 if V081201b == 56


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by state (absolute difference between Democratic and Republican candidates)

gen margin = .

replace margin = 21.57648 if V081201b == 1
replace margin = 21.53514 if V081201b == 2
replace margin = 8.519997 if V081201b == 4
replace margin = 19.85125 if V081201b == 5
replace margin = 24.05778 if V081201b == 6
replace margin = 8.952713 if V081201b == 8
replace margin = 22.36737 if V081201b == 9
replace margin = 24.99564 if V081201b == 10
replace margin = 85.92455 if V081201b == 11
replace margin = 2.817986 if V081201b == 12
replace margin = 5.214336 if V081201b == 13
replace margin = 45.01671 if V081201b == 15
replace margin = 25.4261 if V081201b == 16
replace margin = 25.13719 if V081201b == 17
replace margin = 1.032005 if V081201b == 18
replace margin = 9.53476 if V081201b == 19
replace margin = 14.96029 if V081201b == 20
replace margin = 16.23091 if V081201b == 21
replace margin = 18.62981 if V081201b == 22
replace margin = 17.32172 if V081201b == 23
replace margin = 25.44483 if V081201b == 24
replace margin = 25.62824 if V081201b == 25
replace margin = 16.47354 if V081201b == 26
replace margin = 10.23736 if V081201b == 27
replace margin = 13.17464 if V081201b == 28
replace margin = .1334267 if V081201b == 29
replace margin = 2.263988 if V081201b == 30
replace margin = 14.93359 if V081201b == 31
replace margin = 12.49256 if V081201b == 32
replace margin = 9.605469 if V081201b == 33
replace margin = 15.5682 if V081201b == 34
replace margin = 15.12845 if V081201b == 35
replace margin = 28.83973 if V081201b == 36
replace margin = .3288689 if V081201b == 37
replace margin = 8.629559 if V081201b == 38
replace margin = 4.593689 if V081201b == 39
replace margin = 31.29016 if V081201b == 40
replace margin = 16.34782 if V081201b == 41
replace margin = 10.31847 if V081201b == 42
replace margin = 27.80616 if V081201b == 44
replace margin = 8.977085 if V081201b == 45
replace margin = 8.411545 if V081201b == 46
replace margin = 15.06842 if V081201b == 47
replace margin = 11.76924 if V081201b == 48
replace margin = 28.17812 if V081201b == 49
replace margin = 37.00645 if V081201b == 50
replace margin = 6.298969 if V081201b == 51
replace margin = 17.17659 if V081201b == 53
replace margin = 13.12059 if V081201b == 54
replace margin = 13.90412 if V081201b == 55
replace margin = 32.20428 if V081201b == 56


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen time = V082003b

* Gender

gen male = V081101

recode male (2=0)

* Age

gen age = V081104

recode age (-9=.)
recode age (-8=.)
recode age (17=18)

* Education

gen education = V083218x

recode education (-9=.)
recode education (-8=.)
recode education (0=.)

* Interest for election

gen interest = max(V083001a,V083001b)

recode interest (-9=.)
recode interest (-1=.)

recode interest (4=5)
recode interest (2=1)

replace interest = 6 - interest

replace interest = 2 if interest == 3
replace interest = 3 if interest == 5

* Caring about outcome (national)

gen care_whole = V083072

recode care_whole (-9=.)
recode care_whole (-8=.)
recode care_whole (3=0)

* Closeness of election (national)

gen closeness_whole = V083074

recode closeness_whole (-9=.)
recode closeness_whole (-8=.)
recode closeness_whole (-1=.)

recode closeness_whole (1=0)
recode closeness_whole (5=1)

* Closeness of election (state)

gen closeness_state = V083076

recode closeness_state (-9=.)
recode closeness_state (-8=.)
recode closeness_state (-1=.)

recode closeness_state (1=0)
recode closeness_state (5=1)

* Party identification scale (national)

gen pidscale = V083098x

recode pidscale (-1=.)

replace pidscale = pidscale + 1

gen pidscale_whole = 8 - pidscale

label define pidscale 1 "Strong loser PID" 2 "Weak loser PID" 3 "Leaner loser PID" 4 "No PID" 5 "Leaner winner PID" 6 "Weak winner PID" 7 "Strong winner PID"
label values pidscale_whole pidscale

* Party identification scale (state)

gen pidscale_state = pidscale if statewinner == 2
replace pidscale_state = 8 - pidscale if statewinner == 1

label values pidscale_state pidscale

* News attentiveness

gen news = max(V085012,V085023)

recode news (-2=.)
recode news (-1=.)

replace news = 6 - news

* Nancy Pelosi - Correct answer (Speaker of the House of Representatives) = 1, Incorrect answer = 0

gen pelosi = V085120_imported

* Dick Cheney - Correct answer (U.S. vice-president) = 1, Incorrect answer = 0

gen cheney = V085121_imported

* Gordon Brown - Correct answer (British Prime Minister) = 1, Incorrect answer = 0

gen brown = V085122_imported

* John Roberts - Correct answer (Supreme Court Chief Justice) = 1, Incorrect answer = 0

gen roberts = V085123_imported

* Party with the most members in the U.S. House before election - Correct answer (Democratic) = 1, Incorrect answer = 0

gen house = V085066

recode house (-9=.)
recode house (-2=.)

recode house (-8=0)
recode house (5=0)

* Party with the most members in the U.S. Senate before election - Correct answer (Republican) = 1, Incorrect answer = 0

gen senate = V085067

recode senate (-9=.)
recode senate (-2=.)

recode senate (-8=0)
recode senate (1=0)
recode senate (5=1)

* Political knowledge index

gen knowledge = pelosi + cheney + brown + roberts + house + senate

* Political discussion

gen discussion = max(V085108a,V085109)

recode discussion (-9=.)
recode discussion (-8=.)
recode discussion (-2=.)
recode discussion (-1=.)

replace discussion = 0 if V085108 == 5

gen discussion_3pts = 0 if discussion < 2 & discussion!=.
replace discussion_3pts = 1 if discussion >= 2 & discussion < 4 & discussion!=.
replace discussion_3pts = 2 if discussion >= 4 & discussion!=.


*********************************
** NATIONAL-LEVEL EXPECTATIONS **
*********************************

* Forecasts (1 = Democrat, 3 = Republican, 7 = other)

gen forecast_whole = V083073

recode forecast_whole (-9=.)

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=1 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 1


******************************
** STATE-LEVEL EXPECTATIONS **
******************************

* Forecasts (1 = Democrat, 2 = Republican, 3 = other)

gen forecast_state = V083075

recode forecast_state (-9=.)

recode forecast_state (3=2)
recode forecast_state (7=3)

* Identify correct forecasts

gen correct_state_d = 0 if forecast_state!=statewinner & forecast_state!=.
replace correct_state_d = 1 if forecast_state == statewinner & forecast_state!=.


**********
** SAVE **
**********

save "us2008_anes.dta", replace


*******************
** MAIN ANALYSES **
*******************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d discussion pidscale_whole interest news knowledge i.care_whole i.closeness_whole age i.male education time
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1

* Regression analysis and percentage of correct and incorrect forecasts (state)

melogit correct_state_d discussion pidscale_state interest news knowledge i.closeness_state age i.male education time margin || V081201b: , or
estimates table, star(.05 .01 .001)

tab correct_state_d if e(sample) == 1


************************
** SUMMARY STATISTICS **
************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_state_d "State winner (DV)"
label variable discussion "Discussion"
label variable pidscale_whole "National PID"
label variable pidscale_state "State PID"
label variable interest "Interest"
label variable news "News attentiveness"
label variable knowledge "Political knowledge"
label variable care_whole "Caring about outcome"
label variable closeness_whole "Perceived closeness"
label variable closeness_state "Perceived closeness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"
label variable margin "Margin"

quietly logistic correct_whole_d discussion pidscale_whole interest news knowledge i.care_whole i.closeness_whole age i.male education time

estpost tabstat correct_whole_d discussion pidscale_whole interest news knowledge care_whole closeness_whole age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2008_anes_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2008 American presidential election (ANES), national level") replace

eststo clear

quietly melogit correct_state_d discussion pidscale_state interest news knowledge i.closeness_state age i.male education time margin || V081201b: , or

estpost tabstat correct_state_d discussion pidscale_state interest news knowledge closeness_state age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2008_anes_summary_state.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2008 American presidential election (ANES), state level") replace

eststo clear

log close